/**
 * 
 */
package com.primestone.cim.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * @author cesar.mateus
 *
 */
public class DateUtil {
	
	/**
	 * 
	 */
	public static final int FREQ_NONE = 0;
	
	/**
	 * 
	 */
	public static final int FREQ_MINUTE = 1;
	
	/**
	 * 
	 */
	public static final int FREQ_HOUR = 2;
	
	/**
	 * 
	 */
	public static final int FREQ_DAY = 3;
	
	/**
	 * 
	 */
	public static final int FREQ_WEEK = 4;
	
	/**
	 * 
	 */
	public static final int FREQ_MONTH = 5;
	
	/**
	 * 
	 */
	public static final GregorianCalendar MIN_DATE_CAL = new GregorianCalendar(1000, 01, 01);
	
	/**
	 * 
	 */
	public static final GregorianCalendar MAX_DATE_CAL = new GregorianCalendar(3000, 01, 01);
	
	/**
	 * 
	 */
	public static final Date MIN_DATE = MIN_DATE_CAL.getTime();
	
	/**
	 * 
	 */
	public static final Date MAX_DATE = MAX_DATE_CAL.getTime();
	
	/**
	 * 
	 */
	public static final int MONTHS_BACKWARD = -2;
	
	/**
	 * 
	 */
	public static final XMLGregorianCalendar _NULL_DATE_ = null;
	
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd_HHmmss");

	/**
	 * 
	 * @return
	 */
	public static XMLGregorianCalendar getXMLGregorianCalendarFromGregorianCalendar() {
		//		String nowString;
		//		GregorianCalendar nowGregorianCalendar = null;
		//		try {
		//			nowString = ISO8601.fromCalendar(Calendar.getInstance());
		//			nowGregorianCalendar = ISO8601.toGregorianCalendar(nowString);
		//		} catch (ParseException e) {
		//			e.printStackTrace();
		//		}

		XMLGregorianCalendar xmlGregorianCalendar = null;
		try {
			//xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
			xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(new GregorianCalendar());
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
		return xmlGregorianCalendar;
	}

	/**
	 * 
	 * @param gregorianCalendar
	 * @return
	 */
	public static XMLGregorianCalendar getXMLGregorianCalendarFromGregorianCalendar(GregorianCalendar gregorianCalendar) {
		//		String nowString;
		//		GregorianCalendar nowGregorianCalendar = null;
		//		try {
		//			nowString = ISO8601.fromCalendar(Calendar.getInstance());
		//			nowGregorianCalendar = ISO8601.toGregorianCalendar(nowString);
		//		} catch (ParseException e) {
		//			e.printStackTrace();
		//		}

		XMLGregorianCalendar xmlGregorianCalendar = null;
		try {
			//xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(nowGregorianCalendar);
			xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gregorianCalendar);
		} catch (DatatypeConfigurationException e) {
			e.printStackTrace();
		}
		return xmlGregorianCalendar;
	}
	
	/**
	 * 
	 * @param xmlGregorianCalendar
	 * @return
	 */
	public static Date getJavaUtilDateFromXMLGregorianCalendar(XMLGregorianCalendar xmlGregorianCalendar) {
		return xmlGregorianCalendar.toGregorianCalendar().getTime();
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Date getJavaSqlDateFromGregorianCalendar() {
		return new java.sql.Date( new GregorianCalendar().getTimeInMillis() );
	}
	
	/**
	 * 
	 * @param gregorianCalendar
	 * @return
	 */
	public static java.sql.Date getJavaSqlDateFromGregorianCalendar(GregorianCalendar gregorianCalendar) {
		return new java.sql.Date( gregorianCalendar.getTimeInMillis() );
	}
	
	/**
	 * 
	 * @param javaUtilDate
	 * @return
	 */
	public static java.sql.Date getJavaSqlDateFromJavaUtilDate(Date javaUtilDate) {
		return new java.sql.Date( javaUtilDate.getTime() );
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Date getMinJavaSqlDate() {
		return new java.sql.Date( MIN_DATE.getTime() );
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Date getMaxJavaSqlDate() {
		return new java.sql.Date( MAX_DATE.getTime() );
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Date getJavaSqlDateFromJavaUtilDate() {
		return new java.sql.Date( new java.util.Date().getTime() );
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Timestamp getJavaSqlTimestampFromGregorianCalendar() {
		return new java.sql.Timestamp( new GregorianCalendar().getTimeInMillis() );
	}
	
	/**
	 * 
	 * @param gregorianCalendar
	 * @return
	 */
	public static java.sql.Timestamp getJavaSqlTimestampFromGregorianCalendar(GregorianCalendar gregorianCalendar) {
		return new java.sql.Timestamp( gregorianCalendar.getTimeInMillis() );
	}
	
	/**
	 * 
	 * @param javaUtilDate
	 * @return
	 */
	public static java.sql.Timestamp getJavaSqlTimestampFromJavaUtilDate(Date javaUtilDate) {
		return new java.sql.Timestamp( javaUtilDate.getTime() );
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Timestamp getMinJavaSqlTimestamp() {
		return new java.sql.Timestamp( MIN_DATE.getTime() );
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Timestamp getMaxJavaSqlTimestamp() {
		return new java.sql.Timestamp( MAX_DATE.getTime() );
	}
	
	/**
	 * 
	 * @return
	 */
	public static java.sql.Timestamp getJavaSqlTimestampFromJavaUtilDate() {
		return new java.sql.Timestamp( new java.util.Date().getTime() );
	}
	
	/**
	 * 
	 * @param date
	 * @return
	 */
	public static boolean safe(Date date) {
		if( date == null ) {
			return false;
		}
		return true;
	}
	
	/**
	 * @param date
	 * @return
	 */
	public static boolean safe(Calendar date) {
		if( date == null ) {
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * @param date
	 * @return
	 */
	public static boolean safe(XMLGregorianCalendar date) {
		if( date == null ) {
			return false;
		}
		return true;
	}
	
	/**
	 * 
	 * @param lastRead
	 * @return
	 */
	public static Date getLastReadMonthsAgo(Date lastRead) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(lastRead);
		calendar.add(Calendar.MONTH, DateUtil.MONTHS_BACKWARD);
		return calendar.getTime();
	}
	
	/**
	 * 
	 * @param lastRead
	 * @param monthsBackward
	 * @return
	 */
	public static Date getLastReadMonthsAgo(Date lastRead, int monthsBackward) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(lastRead);
		calendar.add(Calendar.MONTH, -monthsBackward);
		return calendar.getTime();
	}

	/**
	 * @return
	 */
	public static String getFormattedDate() {
		return SDF.format(new Date());
	}
	
	/**
	 * @param date
	 * @return
	 */
	public static String getFormattedDate(Date date) {
		return SDF.format(date);
	}

}
